package com.aude.sharding.jdbc.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.aude.sharding.exception.shardingException;

/**
 * 保存{@link TPreparedStatement}对象创建的方法
 * @author dawei
 *
 */
public enum StatementCreateMethod {
	/*---------------------Statement创建的命令---------------------*/
	createStatement,
	
	/*------------------PreparedStatement创建的命令-----------------*/
	prepareStatement_sql,
	prepareStatement_sql_autoGeneratedKeys,
	prepareStatement_sql_columnIndexes,
	prepareStatement_sql_columnNames;
	
	public PreparedStatement prepareStatement(Connection conn, Object[] args) throws SQLException{
		switch(this){
		case prepareStatement_sql:
			return conn.prepareStatement((String) args[0]);
		case prepareStatement_sql_autoGeneratedKeys:
			return conn.prepareStatement((String)args[0], (Integer)args[1]);
		case prepareStatement_sql_columnIndexes:
			return conn.prepareStatement((String)args[0], (int[])args[1]);
		case prepareStatement_sql_columnNames:
			return conn.prepareStatement((String)args[0], (String[])args[1]);
		default:
			throw new shardingException("not support prepareStatement(xx) method : " + this);
		}
	}
	
	/**
	 * 如果在执行完SQL之后需要调用Statement.getGeneratedKeys()并保存为Statement.generatedKeysResultSet
	 * 
	 * @return
	 */
	public boolean autoGeneratedKeys(){
		switch(this){
		case prepareStatement_sql_autoGeneratedKeys:
		case prepareStatement_sql_columnIndexes:
		case prepareStatement_sql_columnNames:
			return true;
		default:
			return false;
		}
	}
	
}
